package net.lab1024.sa.admin.module.business.datasource.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import net.lab1024.sa.admin.constant.AdminSwaggerTagConst;
import net.lab1024.sa.admin.module.business.datasource.domain.form.DatasourceForm;
import net.lab1024.sa.admin.module.business.datasource.domain.vo.DatasourceVO;
import net.lab1024.sa.admin.module.business.datasource.service.DatasourceService;
import net.lab1024.sa.base.common.domain.PageParam;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.springframework.web.bind.annotation.*;

/**
 * 数据源管理
 *
 * @Author 1024创新实验室-主任:卓大
 * @Date 2024-01-01 00:00:00
 * @Wechat zhuoda1024
 * @Email lab1024@163.com
 * @Copyright <a href="https://1024lab.net">1024创新实验室</a>
 */
@RestController
@Tag(name = "数据源管理")
@RequestMapping("/datasource")
public class DatasourceController {

    @Resource
    private DatasourceService datasourceService;

    @GetMapping("/page")
    @Operation(summary = "分页查询数据源列表 @author 卓大")
    public ResponseDTO<Page<DatasourceVO>> queryPage(@Valid PageParam pageParam,
                                                    @RequestParam(required = false) String name,
                                                    @RequestParam(required = false) String type,
                                                    @RequestParam(required = false) Integer status) {
        Page<DatasourceVO> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        return datasourceService.queryPage(page, name, type, status);
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "根据ID查询数据源详情 @author 卓大")
    public ResponseDTO<DatasourceVO> getDetailById(@PathVariable Long id) {
        return datasourceService.getDetailById(id);
    }

    @PostMapping("/create")
    @Operation(summary = "新增数据源 @author 卓大")
    public ResponseDTO<String> create(@Valid @RequestBody DatasourceForm form) {
        return datasourceService.create(form);
    }

    @PostMapping("/update")
    @Operation(summary = "更新数据源 @author 卓大")
    public ResponseDTO<String> update(@Valid @RequestBody DatasourceForm form) {
        return datasourceService.update(form);
    }

    @PostMapping("/delete/{id}")
    @Operation(summary = "删除数据源 @author 卓大")
    public ResponseDTO<String> delete(@PathVariable Long id) {
        return datasourceService.delete(id);
    }

    @PostMapping("/test-connection")
    @Operation(summary = "测试数据源连接 @author 卓大")
    public ResponseDTO<String> testConnection(@Valid @RequestBody DatasourceForm form) {
        return datasourceService.testConnection(form);
    }
} 